
package com.android.fitnesscenter.view.client;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ClientLimitSettingActivity extends BaseActivity {

    @ViewInject(R.id.edtxt_limit)
    private EditText edtxt_limit;

    @ViewInject(R.id.btn_ok)
    public Button btn_ok;

    private String limitStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.limit_setting_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_limit_setting));
        limitStr = mSharePrefsHelper.getString("minusLimit", "99999.99");
        edtxt_limit.setText(limitStr);
        edtxt_limit.setSelection(limitStr.length());
        edtxt_limit.addTextChangedListener(mTextWatcher);
    }

    private TextWatcher mTextWatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s != null) {
                String temp = s.toString();
                if (!TextUtils.isEmpty(temp)) {
                    if (!temp.startsWith(".")) {
                        float curMoney = Float.parseFloat(temp);
                        if (curMoney >= 99999.99) {
                            curMoney = 99999.99f;
                            edtxt_limit.removeTextChangedListener(this);
                            temp = "" + 99999.99;
                            edtxt_limit.setText(temp);
                            edtxt_limit.setSelection(temp.length());
                            ToastHelper.getInstance().showShortMsg("最大金额为99999.99!");
                            edtxt_limit.addTextChangedListener(mTextWatcher);
                        } else {
                            int posDot = temp.indexOf(".");
                            if (posDot <= 0) return;
                            if (temp.length() - posDot - 1 > 2) {
                                s.delete(posDot + 3, posDot + 4);
                            }
                        }
                    } else {
                        edtxt_limit.removeTextChangedListener(this);
                        temp = "0" + temp;
                        edtxt_limit.setText(temp);
                        edtxt_limit.setSelection(temp.length());
                        edtxt_limit.addTextChangedListener(mTextWatcher);
                    }
                }
            }
        }
    };

    @OnClick(R.id.btn_ok)
    public void btn_ok(View view) {
        if (edtxt_limit != null && edtxt_limit.getText() != null) {
            limitStr = edtxt_limit.getText().toString();
            Loger.e("limit is " + limitStr);
            updatelimit(limitStr);
        }
    }

    private void updatelimit(String minusLimit) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", getIMEI());
        params.addBodyParameter("minusLimit", minusLimit);
        String url = ConfigProperties.SERVICE_URL + "updateLimit.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(ClientLimitSettingActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ClientLimitSettingActivity.this != null && !ClientLimitSettingActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        try {
                            String result = stringResponseInfo.result;
                            Loger.e("result is " + result);
                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject != null) {
                                String msg = jsonObject.getString("message");
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    mSharePrefsHelper.setString("minusLimit", limitStr);
                                    ToastHelper.getInstance().showShortMsg(msg);
                                } else {
                                    new ViewHelper(ClientLimitSettingActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ClientLimitSettingActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                if (ClientLimitSettingActivity.this != null && !ClientLimitSettingActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ClientLimitSettingActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    @Override
    protected void titlebarMethod(int id) {

    }

}
